package com.lyl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyl.DTO.AnswersheetDTO;
import com.lyl.VO.OnlineExam.TBVO;
import com.lyl.common.Msg;
import com.lyl.common.ResultUtil;
import com.lyl.entity.Answersheet;
import com.lyl.entity.Testbank;
import com.lyl.mapper.TestbankMapper;
import com.lyl.service.AnswersheetService;
import com.lyl.service.TestbankService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 答题卡
 * @author LYL
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/Answersheet")
public class AnswersheetController {
    @Autowired
    AnswersheetService answersheetService;

    @Autowired
    TestbankService testbankService;

    @Autowired
    TestbankMapper testbankMapper;
    //查询所有答题卡
    @RequestMapping("/getAnswersheet")
    public Msg getAnswersheet(){
        return null;
    }
    //根据答题卡id查询
    @RequestMapping("/getAnswersheetById/{id}")
    public Msg getAnswersheetById(@PathVariable(name = "id") int id){
        return null;
    }

    //添加答题卡
    @RequestMapping("/save")
    public Msg addAnswersheet(@RequestBody AnswersheetDTO answersheetDTO){
        System.out.println(answersheetDTO);
        List<TBVO> answerList = answersheetDTO.getAnswerList();
        for (TBVO tbvo : answerList) {
            Answersheet answersheet = new Answersheet();
            setScore(tbvo, answersheet,answersheetDTO);
            answersheet.setStuAnswer(tbvo.getAnswer());
            answersheet.setTestId(tbvo.getTestId());
            answersheet.setStuId(answersheetDTO.getStuId());
            answersheet.setTpId(answersheetDTO.getTpId());
            System.out.println(answersheet);
            boolean res = answersheetService.save(answersheet);
            if (!res){
                return ResultUtil.error(400,"答题卡信息有误");
            }
        }
        return ResultUtil.success();
    }

    public Void setScore(TBVO tbvo,Answersheet answersheet,AnswersheetDTO answersheetDTO){
        //将提交的答案分割成数组
        /*String[] split = tbvo.getAnswer().split("");*/
        //获取答案
        QueryWrapper<Testbank> wrapper = new QueryWrapper<>();
        wrapper.inSql("test_id","select test_id from tplist where  tp_id = "+answersheetDTO.getTpId());
        wrapper.eq("test_id",tbvo.getTestId());
        Testbank testbyId = testbankService.getOne(wrapper);
        //将提交的答案分割成数组进行排序
        /*String[] split1 = testbyId.getAnswer().split("");*/
        /*Arrays.sort(split1);*/
        System.out.println("===============");
        System.out.println("学生答案："+tbvo.getAnswer());
        System.out.println("正确答案："+testbyId.getAnswer());
        System.out.println("================");
        if (tbvo.getAnswer().equals(testbyId.getAnswer())){
            answersheet.setStuScore(testbyId.getScore());
            System.out.println("对");
        }else{
            answersheet.setStuScore(0);
            System.out.println("错");
        }
        return null;
    }

    //删除答题卡
    @RequestMapping("/delAnswersheet/{id}")
    public Msg delAnswersheet(@PathVariable(name = "id") int id){
        return null;
    }
    //修改答题卡信息
    @RequestMapping("/updateAnswersheet")
    public Msg updateAnswersheet(@RequestBody  Answersheet answersheet){
        return null;
    }
}
